/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Common.Utils;
import Model.Dto.Account;
import Model.Dto.jaxb.Accounts.AccountType;
import Model.Dto.jaxb.Accounts.Accounts;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Quang Nhat
 */
public class AccountModel {
    public static Accounts getJAXBAccounts() {
        List<Account> accs = getAllAccount();
        Accounts rs = new Accounts();
        for (Account a : accs) {
            AccountType at = new AccountType();
            at.setAccountName(a.getAccountName());
            at.setAccountNumber(BigInteger.valueOf(a.getAccountNumber()));
            at.setActive(a.isActive());
            rs.getAccount().add(at);
        }
        return rs;
    }
    
    public static List<Account> getAllAccount() {
        List<Account> result = new ArrayList<Account>();
        Connection con = null;
        ResultSet rs = null;
        PreparedStatement stm = null;
        try {
            con = Utils.makeConnection();
            stm = con.prepareStatement("SELECT * FROM Accounts "
                    + "ORDER BY AccountNumber");
            rs = stm.executeQuery();
            while (rs.next()) {
                result.add(new Account(rs.getInt("AccountNumber"), rs.getString("AccountName"), rs.getBoolean("Active")));
            }
        } catch (Exception ex) {
            Utils.ReportException(ex);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException ex) {
                    Utils.ReportException(ex);
                }
            }
            if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    Utils.ReportException(ex);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Utils.ReportException(ex);
                }
            }
        }
        return result;
    }
    
    public static boolean addAccount(Account acc) {
        Connection con = null;
        ResultSet rs = null;
        PreparedStatement stm = null;
        try {
            con = Utils.makeConnection();
            stm = con.prepareStatement("INSERT INTO Accounts "
                    + "VALUES(?, ?, ?)");
            stm.setInt(1, acc.getAccountNumber());
            stm.setString(2, acc.getAccountName());
            stm.setBoolean(3, acc.isActive());
            return stm.executeUpdate() > 0;
        } catch (Exception ex) {
            Utils.ReportException(ex);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException ex) {
                    Utils.ReportException(ex);
                }
            }
            if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    Utils.ReportException(ex);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Utils.ReportException(ex);
                }
            }
        }
        return false;
    }
    
    public static boolean updateAccount(Account acc) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Utils.makeConnection();
            stm = con.prepareStatement("UPDATE Accounts "
                    + " SET AccountName = ?, Active = ? "
                    + " WHERE AccountNumber = ?");
            stm.setString(1, acc.getAccountName());
            stm.setBoolean(2, acc.isActive());
            stm.setInt(3, acc.getAccountNumber());
            return stm.executeUpdate() > 0;
        } catch (Exception ex) {
            Utils.ReportException(ex);
        } finally {
            if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    Utils.ReportException(ex);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Utils.ReportException(ex);
                }
            }
        }
        return false;
    }
}
